﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DigDes.DSchool.SUPS.DataAccess.FileLoaders
{
    public class MyLoader1 : ILoader<MyLoader1DTO>
    {
        // TODO: Реализовать интерфейс загрузчика и логику проверки типа файла/лоигку загрузки файла

        public bool Check(string filePath)
        {
            if (Path.GetExtension(filePath).Equals(".OUT")) return true;
            else {
                Console.WriteLine("Wrong type of file");
                return false;
            }
                
        }

        public IEnumerable<MyLoader1DTO> Load(string filePath)
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                string row;

                while ((row = sr.ReadLine()) != null)
                {
                    var obj = new MyLoader1DTO();
                    obj.Vagonnum = row.Substring(0, 8);
                    obj.Weight = row.Substring(8, 3);
                    obj.DestStation = row.Substring(11, 5);
                    obj.GoodsCode = row.Substring(16, 5);
                    obj.RecieverCode = row.Substring(21, 4);
                    obj.VagonParkType = row.Substring(25, 2);
                    obj.DepatureStation = row.Substring(27, 5);
                    obj.OperationCode = row.Substring(32, 2);
                    obj.OperationDate = row.Substring(34, 4);
                    obj.OperationYear = row.Substring(38, 2);
                    obj.OperationTime = row.Substring(40, 4);
                    obj.StationCommissionOperation = row.Substring(44, 5);
                    obj.RoadDelivery = row.Substring(49, 2);
                    obj.RoadRecieve = row.Substring(51, 2);
                    obj.TrainIndex = row.Substring(53, 13);
                    obj.TrainNum = row.Substring(66, 4);
                    yield return obj;
                }
            }
        }
    }
}
